package com.petalsgroup.petals.service.user.impl;

import com.petalsgroup.petals.dao.user.UserDao;
import com.petalsgroup.petals.dao.user.impl.UserDaoImpl;
import com.petalsgroup.petals.pojo.UserDO;
import com.petalsgroup.petals.service.user.UserService;
import com.petalsgroup.petals.util.MailUtils;
import com.petalsgroup.petals.util.Md5Util;
import com.petalsgroup.petals.util.UuidUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    @Override//注册
    public boolean regist(UserDO user) {
        UserDO userByUsername = userDao.findUserByUsername(user);
        user.setCode(UuidUtil.getCode());
        user.setActive_state("N");
        if (userByUsername!=null){
            return false;
        }else {
            //设置禁言状态
            user.setBanned("N");
            //设置注册时间
            user.setCreate_time(new Date());
            //注册以后
            userDao.register(user);
            //调用邮件工具类 发送邮件
            MailUtils.sendMail(user.getEmail(),"<a href=\"http://localhost:8080/petals/user/uuid?code="+user.getCode()+"\">验证登录</a>","登录验证");

            return true;
        }
    }

    @Override
    public void updateUuid(String code) {
        UserDO uuidUser = userDao.findUuid(code);
        if (uuidUser!=null){
            userDao.updateStatus(code);
        }
    }

    @Override
    public UserDO login(UserDO user) {
        //调用dao 的登录方法
        UserDO loginUser = userDao.login(user.getUsername(),user.getPassword());
        return loginUser;
    }

    @Override
    public Boolean findPassword(UserDO user) {
        UserDO findUser = userDao.findUserByUsernameAndEmail(user);
        if (findUser != null){
            String password = Md5Util.convertMD5(findUser.getPassword());
            //已找回密码，发送邮箱
            MailUtils.sendMail(user.getEmail(),"您的密码是:"+password+"","找回密码");
            return true;
        }else {
            return false;
        }
    }

    @Override
    public UserDO findUserByUid(int uid) {
        return userDao.findUserByUid(uid);
    }
}
